package com.nbsaas.boot.product.data.entity;

import com.nbsaas.boot.code.annotation.*;
import com.nbsaas.boot.jpa.data.entity.AbstractEntity;
import com.nbsaas.boot.jpa.data.entity.CatalogEntity;
import com.nbsaas.boot.rest.filter.Operator;
import jakarta.persistence.*;
import lombok.Data;
import org.hibernate.annotations.Comment;

import java.io.Serializable;
import java.util.List;

/**
 * 商品分类实体
 * 用于管理商品的分类层级结构
 */
@CatalogClass
@Comment("商品分类")
@Data
@FormAnnotation(title = "商品分类管理", model = "商品分类")
@Entity
@Table(name = "nb_product_catalog")
public class ProductCatalog extends CatalogEntity {


    @Comment("父级")
    @FieldName
    @FieldConvert
    @FormField(title = "父级", type = InputType.select, option = "parent")
    @ManyToOne(fetch = FetchType.LAZY)
    private ProductCatalog parent;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "parent")
    private List<ProductCatalog> children;

    @Override
    public Serializable getParentId() {
        if (parent != null) {
            return parent.getId();
        }
        return null;
    }
}
